#include "../thread_pool.h"
#include "../user.h"
#include "mysql.h"
/*-------------------------delimiter-------------------*/
int add_md5_table(user_info* user, const char* buff, int filelen) {
    connect_mysql(user);
    //执行prepare操作，将stmt与模板化的sql语句绑定
    const char* sql = "INSERT INTO md5 (filelength, md5) values(?, ?)";
    int ret = mysql_stmt_prepare(user->stmt, sql, strlen(sql));
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt), mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //获取模板化的sql中占位符的个数
    int count = mysql_stmt_param_count(user->stmt);
    if (count != 2) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    //设置参数
    int filelens = 0;

    char username[100] = { 0 };
    unsigned long name_len = 0;
    //绑定参数
    MYSQL_BIND bind[2];
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type = MYSQL_TYPE_LONG;
    bind[0].buffer = &filelens;
    bind[0].is_null = 0;
    bind[0].length = NULL;//指定字符串长度的缓冲区的首地址

    bind[1].buffer_type = MYSQL_TYPE_VAR_STRING;
    bind[1].buffer = username;
    bind[1].is_null = 0;
    bind[1].length = &name_len;//指定字符串长度的缓冲区的首地址

    //执行绑定操作
    ret = mysql_stmt_bind_param(user->stmt, bind);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }

    //传递参数
    filelens = filelen;
    strcpy(username, buff);
    name_len = strlen(username);

    //执行EXECUTE语句
    ret = mysql_stmt_execute(user->stmt);
    if (ret) {
        printf("(%d, %s\n)", mysql_stmt_errno(user->stmt),
               mysql_stmt_error(user->stmt));
        mysql_close(user->pconn);
        exit(1);
    }
    else {
        printf("QUERY OK, %ld row affected.\n",
               mysql_stmt_affected_rows(user->stmt));
        return 1;
    }
}

